package com.example.hy2110szyx.controller;

import com.example.hy2110szyx.common.DatatablesJSON;
import com.example.hy2110szyx.common.MyJsonResult;
import com.example.hy2110szyx.entity.UserInfo;
import com.example.hy2110szyx.service.UserService;
import com.example.hy2110szyx.vo.PageVo;
import com.github.pagehelper.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.UUID;

@RestController
@Slf4j
@RequestMapping("/user")
@CrossOrigin(origins = "*")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/query")
    public DatatablesJSON<UserInfo> query(
            int draw,
            @RequestParam("order[0][column]") int orderColumn,
            @RequestParam("order[0][dir]") String orderDir,
            int start, int length, String uno, String username, Integer role
    ) {
        String[] columns = {null, "uno", "username", null, null, null};
        UserInfo userConn = new UserInfo(uno, username, role);
        log.debug("order[0][column]=" + orderColumn);
        log.debug("order[0][dir]=" + orderDir);
        log.debug("userConn is {}", userConn);
        PageVo<UserInfo> pageVo = new PageVo(start, length, columns[orderColumn], orderDir, userConn);
        Page<UserInfo> page = userService.queryAllUsersForPage(pageVo);
        DatatablesJSON<UserInfo> datatablesJSON = new DatatablesJSON<>();
        datatablesJSON.setDraw(draw);
        datatablesJSON.setRecordsFiltered(page.getTotal());
        datatablesJSON.setData(page.getResult());
        return datatablesJSON;
    }

    /**
     * 添加用户
     *
     * @param user
     */
    @PostMapping("/add")
    @ResponseStatus(HttpStatus.CREATED)
    public MyJsonResult addOne(@RequestBody UserInfo user) {
        UserInfo existsUser = null;
        String uno = null;//用户编号
        UserInfo userInfo = userService.queryByUsername(user.getUsername());
        if (null != userInfo) {
            return new MyJsonResult(1, "该用户名已被注册", null, null);
        }
        do {
            //防止用户编号重复
            uno = (int)((Math.random()*9+1)*1000000) + "";
            existsUser = userService.queryByUno(uno);
        } while (null != existsUser);
        user.setUno(uno);
        userService.regist(user);
        return new MyJsonResult(0, "添加成功", null, user);
    }

    @PutMapping("/mod/{id}")
    @ResponseStatus(HttpStatus.OK)
    public MyJsonResult modOne(@PathVariable Integer id, @RequestBody UserInfo userInfo) {
        log.debug("userInfo is {}", userInfo);
        userService.modOne(userInfo);
        return new MyJsonResult(0, "修改成功", null, userInfo);
    }

    @DeleteMapping("/delsome")
    @ResponseStatus(HttpStatus.OK)
    public MyJsonResult delSome(@RequestBody Integer[] ids) {
        userService.deleteSomeById(ids);
        return new MyJsonResult(0, "删除成功", null, null);
    }

    @PatchMapping("/modrole/{id}/{role}")
    @ResponseStatus(HttpStatus.OK)
    public MyJsonResult modRole(@PathVariable("id") Integer id, @PathVariable("role") Integer role, HttpSession session) {
        UserInfo loginUser = (UserInfo) session.getAttribute("loginUser");
        log.info("loginUser is {}", loginUser);
        if (loginUser.getRole() < role) {
            return new MyJsonResult(1, "您不能赋予角色比自己更高的权限", null, null);
        } else {
            userService.modRoleById(id, role);
            return new MyJsonResult(0, "修改权限成功", null, null);
        }
    }
}
